package com.aptemo.webetek.client.widgets.stakes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.impl.GetBetsAction;
import com.aptemo.webetek.client.commands.responses.GotBetListResponse;
import com.aptemo.webetek.client.enums.EnumBetStatus;
import com.aptemo.webetek.client.events.StakeSelectedEvent;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.widgets.LoginProposalWidget;
import com.aptemo.webetek.client.widgets.paging.PagingController;
import com.aptemo.webetek.shared.BetBean;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

public class StakesPresenter
		extends PagingController<BetBean>
{

	private HandlerManager			meventbus;
	private WebetekUserServiceAsync	mservice;
	protected boolean				misAdmin	= false;
	private StakesDisplay			display;
	private int						pageNo		= 0;
	private boolean					hidePaging;
	protected ArrayList<BetBean>	list;
	private String					teamId;

	private EnumBetStatus			status		= EnumBetStatus.OPENED;
	private HasWidgets				container;

	public StakesPresenter(StakesDisplay stakeHistory, HandlerManager eventbus, WebetekUserServiceAsync userService, String teamId)
	{
		this(stakeHistory, eventbus, userService, teamId, true, 0);
	}

	public StakesPresenter(StakesDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String teamId, boolean hidePaging, int pageNo)
	{
		super(view.getPagingView(), 5);

		this.meventbus = eventbus;
		this.mservice = userService;
		this.display = view;
		this.pageNo = pageNo;
		this.hidePaging = hidePaging;
		this.teamId = teamId;
	}

	private void bind()
	{
		display.setTarget(teamId);

		display.setStatusValue(status);

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				int row = display.getClickedRow(event);
				BetBean stake = getItem(row);
				// update bet slip
				meventbus.fireEvent(new StakeSelectedEvent(stake.getId(), teamId));
			}
		});

		display.getStatusFilter().addChangeHandler(new ChangeHandler()
		{

			@Override
			public void onChange(ChangeEvent event)
			{
				status = display.getStatusFilterValue();

				renderList();
			}
		});
	}

	private void retrieveList()
	{
		display.getInfoField().setText(display.translate("loading"));

		GetBetsAction action = new GetBetsAction(teamId);

		mservice.invoke(action, new GotBetListResponse()
		{

			@Override
			public void got(ArrayList<BetBean> list)
			{
				// sorting...
				Collections.sort(list);

				StakesPresenter.this.list = list;

				// display.getInfoField().setText("[" + list.size() + "]");
				display.getInfoField().setText("");
				renderList();
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				if (code == EErrorCodes.LOGIN_ERROR)
				{
					display.getInfoField().setText("");
					container.clear();
					container.add(new LoginProposalWidget());
				}
				else
				{
					display.getInfoField().setText(message);
				}
			}

		});
	}

	protected void renderList()
	{
		ArrayList<BetBean> stakes = new ArrayList<BetBean>();
		for (BetBean stake : list)
		{
			if (status == null || status.equals(stake.getStatus()))
			{
				stakes.add(stake);
			}
		}

		setList(stakes, 0);
	}

	@Override
	public void go(HasWidgets container)
	{
		this.container = container;
		container.clear();
		container.add(display.getDisplay());

		retrieveList();

		bind();
	}

	@Override
	public void refreshPaging(List<BetBean> data)
	{
		display.updateStakeList(data);
	}

	public void setPageNo(int pageNo)
	{
		this.pageNo = pageNo;
	}

}
